package com.dycx.cms.converter;

import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.common.vendor.oss.AliyunOss;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.cms.entity.ArticleContentDO;
import com.dycx.cms.vo.ArticleContentVO;
import com.dycx.cms.dto.ArticleContentDTO;

/**
 * 文章详情(ArticleContent)实体转换类
 *
 * @author wuzongbing
 * @since 2025-02-13 01:30:39
 */


@Data
public class ArticleContentConverter implements Serializable {
    public static ArticleContentDO dtoToEntity(ArticleContentDTO dto) {
        if (dto == null) return null;
        ArticleContentDO entity = BeanUtil.toBean(dto, ArticleContentDO.class);
        entity.setContent(AliyunOss.delDomain(entity.getContent(), true));
        return entity;
    }

    public static ArticleContentVO entityToVo(ArticleContentDO entity) {
        if (entity == null) return null;
        ArticleContentVO vo = BeanUtil.toBean(entity, ArticleContentVO.class);
        vo.setContent(AliyunOss.addDomain(vo.getContent(), true));
        return vo;
    }

    public static List<ArticleContentVO> entityToVoList(List<ArticleContentDO> entityList) {
        List<ArticleContentVO> voList = new ArrayList<>();
        for (ArticleContentDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<ArticleContentVO> entityToVoPage(Page<ArticleContentDO> page) {

        List<ArticleContentVO> newRecords = new ArrayList<>();

        List<ArticleContentDO> records = page.getRecords();
        for (ArticleContentDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<ArticleContentVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
